import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

// 解决 vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

let router = []

let docs = [
	{ path: '/', component: () => import(/*webpackChunkName:'overview'*/'./views/home') },
	{ path: 'overview', component: () => import(/*webpackChunkName:'overview'*/'./views/overview') },
	{ path: 'post-prepare', component: () => import(/*webpackChunkName:'post-prepare'*/'./views/post-prepare') },
	{ path: 'log', component: () => import(/*webpackChunkName:'log'*/'./views/log') },
	{ path: 'mesh', component: () => import(/*webpackChunkName:'mesh'*/'./views/mesh') },
	{ path: 'mesh-multi-cluster', component: () => import(/*webpackChunkName:'mesh-multi-cluster'*/'./views/mesh-multi-cluster') },
	{ path: 'audit', component: () => import(/*webpackChunkName:'audit'*/'./views/audit') },
	{ path: 'monitor', component: () => import(/*webpackChunkName:'monitor'*/'./views/monitor') },
	{ path: 'prometheus', component: () => import(/*webpackChunkName:'prometheus'*/'./views/prometheus') },
	{ path: 'local-run', component: () => import(/*webpackChunkName:'local-run'*/'./views/local-run') },
	{ path: 'docker-run', component: () => import(/*webpackChunkName:'docker-run'*/'./views/docker-run') },
	{ path: 'k8s-run', component: () => import(/*webpackChunkName:'k8s-run'*/'./views/k8s-run') },
	{ path: 'hpa', component: () => import(/*webpackChunkName:'hpa'*/'./views/hpa') },
	{ path: 'devops', component: () => import(/*webpackChunkName:'devops'*/'./views/devops') },
	{ path: 'tke-cluster', component: () => import(/*webpackChunkName:'tke-cluster'*/'./views/tke-cluster') },
	{ path: 'tcr', component: () => import(/*webpackChunkName:'tcr'*/'./views/tcr') },
]

import layout from './layout'

router.push(
	{
		path: '/',
		component: layout,
		children: docs
	},
)
let routers = new Router({
	routes: router,
	mode: 'history',
	scrollBehavior(to, from, savedPosition) {
		let result = {}
		let hash = decodeURIComponent(to.hash)
		if (hash) {
			result = { selector: hash, behavior: 'smooth' }
		} else {
			result = savedPosition || { x: 0, y: 0 }
		}
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				resolve(result)
			}, 500)
		})
	}
})


routers.beforeEach(function (to, from, next) {
	// loading.start('line');
	next()
})
routers.afterEach((to, from, next) => {
	// (process.env.NODE_ENV == 'development') &&
	// loading.finish();
});
export default routers